var $dial=$('#dial');
var $infoLeft=$('#infoLeft');
function _pressTel(){
    $dial.off('touchend').on('touchend',function(e){
        var $this=$(this);
        var tel=$infoLeft.find('.tel-info').html();
        $this.attr('href','tel:'+tel);
    });
}
common.goBackNext();
var $contactName=$infoLeft.find('.contactName');
var $contactTel=$infoLeft.find('.contactTel');
var $headImg=$infoLeft.children('p').find('img');
var clockBtn=$('#clock-btn');
var $bubble=$('.bubble');
var $door=$('.door');
var imageHost=common.imageHost;
var rewardId=common.getQueryString('id');
var token=common.getToken();
var agentInfo=common.getLocalData('H5-agentInfo');
var globalHost=common.globalHost;
var imageHost=common.imageHost;
var getRewardDetailUrl=globalHost+common.getRewardDetail;
var openLockUrl=globalHost+common.openLock;
var token=common.getToken();
var sendData={
    rewardId:rewardId,
    agentId:agentInfo.id,
    token:token
};
requestData({
    sendData:sendData,
    url:getRewardDetailUrl,
    callBack:function(data){
        var data=data.res.data.record;
        data['photo']?$headImg.attr('src',imageHost+data['photo']):'';
        data['contactName']?$contactName.html(data['contactName']):'';
        data['contactTel']?$contactTel.html(data['contactTel']):'';
        _pressTel();
        var sendContent={
            houseId:data.houseId,
            agentId:agentInfo.id,
            token:token
        };
        data['lockFixStatus']==1?_pressLock(clockBtn,sendContent,openLockUrl):clockBtn.hide()&&clockBtn.next().show(); 
    }
});
function _pressLock(clockBtn,sendContent,openLockUrl){
    clockBtn.off('hold').on('hold',function(e){
        var $this=$(this);
        if(!$this.hasClass('cur')){
            $this.addClass('cur');
            requestData({
                sendData:sendContent,
                url:openLockUrl,
                callBack:function(data){
                    data=typeof data==="string"?data=JSON.parse(data):data;
                    if(data.code==101 ||data.code==102){
                        console.log("token为空或者token失效");
                        window.location.href='login.html';
                    };
                    if(data['code']==0){
                        $bubble.addClass('cur');
                        $door.addClass('cur');
                        setTimeout(function () {
                            $this.removeClass('cur');
                            $bubble.removeClass('cur');
                            $door.removeClass('cur');
                        }, 3000);
                    }else{
                        common.errDialog("开锁失败"); 
                        setTimeout(function () {
                           window.location.href="askDoorPassStart.html?id="+rewardId;
                        }, 3000)
                    }
                }
            });  
        }
        e.preventDefault();
        return false;
    });
};
function requestData(options) {
    var sendData=options.sendData;
    var url=options.url;
    var callBack=options.callBack;
    $.ajax({
        data:sendData,
        type: "POST",
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        dataType:"json",
        url:url,
        crossDomain:true,
        success:function(data){
            if(typeof data==="string"){
                data=JSON.parse(data);
            }
            if(data.code==101 ||data.code==102){
                console.log("token为空或者token失效");
                window.location.href='login.html';
            };
            callBack(data);
        },
        timeout:10000,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            common.errDialog("网络不畅，请稍后重试"); 
            setTimeout(function () {
                window.location.href="askDoorPassStart.html?id="+rewardId;
            }, 3000);    
        }
    });
};